import ImageModel from "../../models/Image";
import PageModel from "../../models/Page";
import Crypt from "../../utils/Crypt";
import Avatar from "../../utils/Avatar";
import UserModel from "../../models/User";

const imageModel = new ImageModel()
const pageModel = new PageModel()
const userModel = new UserModel()

Page({
  data: {
    image: '',
    swiper1: [],
    swiper2: [],
    user: {}
  },


  onLoad: function (options) {

    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline'],
      success: () => {
        // console.log('ok')
        // 可以把分享记录保存到数据库
      }
    })

    // bg image
    imageModel.getRemoteImage().then(data => {
      const image = data.bg.center
      this.setData({
        image
      })
    })

    // uni , swiper
    pageModel.getCenter().then(response => {
      let { data } = response
      data = Crypt.parse(data)
      this.setData({ ...data })
    })

  },

  onShow() {

    // get user info
    userModel.getInfo().then(response => {
      let { data } = response
      data = Crypt.parse(data)
      // console.log(data)

      if (!data.mobile) {
        wx.redirectTo({
          url: '/sub_main/pages/user/mobile/index'
        })
      } else if (!data.avatar || !data.nickname) {
        // 这是老的, 只能获取灰色头像,
        // 现在用新的
        // wx.redirectTo({
        //   url: '/sub_main/pages/user/get-info/index'
        // })

        // 不能太强制了, 审核可能不会通过
        const key = 'update_avatar'
        const is = wx.getStorageSync(key)
        if (!is) {
          wx.setStorageSync(key, true)
          wx.redirectTo({
            url: '/sub_main/pages/user/avatar/index'
          })
        } else {
          this.setData({ user: data })
        }

      } else {
        // if (data.base64Image && data.base64Image.length > 0) {
        //   data.avatar = data.base64Image
        // }
        //
        // this.setData({ user: data })
        data.avatar = Avatar(data)
        // console.log(data.avatar)
        this.setData({ user: data })
      }
    })

  }
});
